import { ShelfStudyPlan } from '@/api/Shelf';
import { CityPicker } from '@/components/CityPicker';
import { TradeCascaderSelect } from '@/components/TradeCascaderSelect';
import { useReactive } from 'ahooks';
import { App, Cascader, Drawer } from 'antd';
import { useState } from 'react';
import type { YmColumnsType } from 'ym-design';
import { YmTable } from 'ym-design';
import { StudyPlanDetail, StudyPlanDetailPorps } from '../Detail';
type RecordType = ShelfStudyPlan;

export function List() {
  const [columns] = useState<YmColumnsType<RecordType>>([
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      width: 200,
    },
    {
      title: '组织类型',
      dataIndex: 'entTypeCodeText',
      key: 'entTypeCodeText',
      width: 100,
    },
    {
      title: '区域',
      dataIndex: 'regionName',
      key: 'regionName',
      ellipsis: true,
      width: 250,
    },
    {
      title: '行业',
      dataIndex: 'tradeName',
      ellipsis: true,
      key: 'tradeName',
      width: 250,
    },
    {
      key: '_operate',
      title: '操作',
      width: 100,
    },
  ]);
  const [tableDep, _reFreshTable] = useState<number>();
  const reFreshTable = () => _reFreshTable(Math.random());

  const upperExtram = useReactive<{ guid?: string }>({ guid: undefined });
  const { modal } = App.useApp();
  const detailsPorps = useReactive<{ props?: StudyPlanDetailPorps }>({});
  return (
    <div className="safe_boss_page">
      <YmTable
        searchForm={{
          formItems: [
            {
              label: '名称',
              name: 'name',
              valueType: {
                type: 'input',
              },
            },
            {
              label: '组织类型',
              name: 'entTypeCode',
              valueType: {
                type: 'select',
                selectProps: {
                  allowClear: true,
                  placeholder: '全部',
                  options: [
                    {
                      value: 'government',
                      label: '政府',
                    },
                    {
                      value: 'association',
                      label: '协会',
                    },
                    {
                      value: 'qiye',
                      label: '企业',
                    },
                  ],
                },
              },
            },
            {
              label: '行业',
              name: 'trade',
              // children: <TradeSelect allowClear placeholder="全部" />,
              children: <TradeCascaderSelect placeholder="全部" />,
            },
            {
              label: '区域',
              name: 'region',
              children: (
                <CityPicker
                  showCheckedStrategy={Cascader.SHOW_CHILD}
                  multiple
                  allowClear
                  placeholder="全部"
                  maxTagCount={1}
                />
              ),
            },
          ],
          span: 6,
        }}
        columns={columns}
        request={async ({ page }, form) => {
          const { region, ...params } = form;
          const { current = 0, pageSize = 10 } = page ?? {};
          const data = await ShelfStudyPlan.onShelfList(current, pageSize, {
            ...params,
            region: region
              ?.map((v: string[]) => {
                return v[v.length - 1];
              })
              .join(','),
          });

          console.log('data', data);
          return data;
        }}
        requestOptions={{
          reFreshDep: [tableDep],
          defaultPageSize: 20,
        }}
        rowKey="guid"
        operate={[
          {
            text: '详情',
            key: 'detail',
            onClick(record) {
              detailsPorps.props = {
                guid: record.guid,
                name: record.name,
                createTime: record.createTime,
                shelfInfo: {
                  area: record.area,
                  org: record.entTypeCodeText,
                  showTrade: record.showTradeArry,
                },
              };
            },
          },
          {
            text: '下架',
            key: 'listing',
            onClick(record) {
              upperExtram.guid = record.guid;
              modal.confirm({
                title: '提示',
                content: `确定要下架【${record.name}】计划模版吗？`,
                async onOk() {
                  await ShelfStudyPlan.shelf.down(record.guid);
                  reFreshTable();
                },
              });
            },
          },
        ]}
      />
      <Drawer
        width={800}
        open={Boolean(detailsPorps.props)}
        title="详情"
        onClose={() => {
          detailsPorps.props = undefined;
        }}
      >
        <StudyPlanDetail {...detailsPorps.props} />
      </Drawer>
    </div>
  );
}
